/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package supermarket;

import java.util.*;
import javax.swing.SwingWorker;
import model.Customer;
import threads.ThreadManager;

/**
 *
 * @author koag
 */
public class SimulatePurchasesPanel extends javax.swing.JPanel {

    private static final long serialVersionUID = 1L;
    private DBmanager db;
    private MainForm parent;
    private int threadsNum;
    private boolean simulationIsActive;

    public SimulatePurchasesPanel(DBmanager db, MainForm parent) {
        this.parent = parent;
        this.db = db;
        initComponents();
        this.populateThreadsCombo();


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        messageArea = new javax.swing.JTextArea();
        b_startSimulation = new javax.swing.JButton();
        jToolBar1 = new javax.swing.JToolBar();
        threadsNumberCombo = new javax.swing.JComboBox();

        jPanel1.setBackground(new java.awt.Color(248, 127, 7));
        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 24)); // NOI18N
        jLabel1.setText("Προσομοίωση αγορών ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jLabel2.setText("Αριθμός Νημάτων");

        messageArea.setColumns(20);
        messageArea.setRows(5);
        jScrollPane1.setViewportView(messageArea);

        b_startSimulation.setText("Εκκίνηση Προσομείωσης");
        b_startSimulation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_startSimulationActionPerformed(evt);
            }
        });

        jToolBar1.setRollover(true);

        threadsNumberCombo.setFocusable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(threadsNumberCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(90, 90, 90)
                        .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(46, 46, 46)
                        .addComponent(b_startSimulation)
                        .addGap(5, 5, 5)))
                .addGap(5, 5, 5))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(b_startSimulation)
                        .addComponent(threadsNumberCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
                .addGap(5, 5, 5))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void populateThreadsCombo() {
        // Παίρνουμε όλους τους πελάτες
        List<Customer> customers = db.getAllCustomerList();
        // Βγάζουμε αυτούς που δεν έχουν πιστωτική
        for (int i = 0; i < customers.size(); i++) {
            if (customers.get(i).getCreditCard() == null) {
                customers.remove(i);
            }
        }     
        
        //Βάζουμε τα στοιχεία στο Combo 
        for (int i = 0; i < customers.size(); i++) {
            this.threadsNumberCombo.addItem(i + 1);
        }
    }

    private void b_startSimulationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_startSimulationActionPerformed
        if (simulationIsActive) {
            return;
        }
        // Αριθμός νημάτων
        threadsNum = (Integer) this.threadsNumberCombo.getSelectedItem();


        // Δημιούργησε ένα νέο SwingWorker για την εκτέλεση των ενεργειών των
        // νημάτων, ώστε να είναι δυνατή η ταυτόχρονη εγγραφή μηνυμάτων στο
        // γραφικό περιβάλλον όσο η διαδικασία εκτελείται
        SwingWorker<Void, Void> swingWorker = new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() throws Exception {

                messageArea.setText("");
                messageArea.append("Δημιουργία νημάτων και Τυχαία επιλογή πελατών για κάθε νήμα...");
                // Δημιουργία του ThreadManager και σύνδεση πελατών
                ThreadManager tm = new ThreadManager(db, threadsNum);
                messageArea.append("[OK]\n\n");

                // Εισαγωγή προϊόντων στο καλάθι αγορών του κάθε πελάτη
                messageArea.append("Εισαγωγή προϊόντων στο καλάθι αγορών του κάθε πελάτη...");
                tm.populateCustomerBasket();
                messageArea.append("[OK]\n\n");

                // Εκκίνηση 
                messageArea.append("Εκκίνηση  νημάτων...\n\n");
                messageArea.append("Περιμένετε μέχρι να ολοκληρωθεί η προσομοίωση...\n\n");
                tm.startAllThreads();
                
                // Τερματισμός
                tm.terminateThreads();

                // Εκτύπωση των αποτελεσμάτων
                messageArea.append("Η προσομοίωση ολοκληρώθηκε...\n");
                messageArea.append("========================================\n");
                messageArea.append("Συνολικά νήματα            : " + threadsNum + "\n");
                messageArea.append("Επιτυχημένες συναλλαγές    : " + tm.getSubmittedNum() + "\n");
                messageArea.append("Μη επιτυχημένες συναλαγές  : " + tm.getNotSubmittedNum() + "\n");
                simulationIsActive = false;
                // Εγγραφή στο αρχείο purchases.xml
                messageArea.append("Εγγραφή των αγορών στο αρχείο purchases.xml...");
                  tm.writeXMLFile();
                messageArea.append("[OK]\n");
                return null;
            }
        };

        // Εκκίνηση της προσομοίωσης
        simulationIsActive = true;
        swingWorker.execute();







    }//GEN-LAST:event_b_startSimulationActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b_startSimulation;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTextArea messageArea;
    private javax.swing.JComboBox threadsNumberCombo;
    // End of variables declaration//GEN-END:variables
}
